package com.rework.joss.ztree.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rework.joss.ztree.BaseStaticTreeNode;
import com.rework.joss.ztree.StaticTree;
import com.rework.joss.ztree.TreeNode;
import com.rework.joss.ztree.dao.SysNodeDAO;
import com.rework.joss.ztree.dao.SysUserDAO;
import com.rework.joss.ztree.entity.SysNode;
import com.rework.joss.ztree.entity.SysUser;

@Transactional
@Service
public class SysTreeService {

	@Autowired
	private SysUserDAO sysUserDAO;
	@Autowired
	private SysNodeDAO sysNodeDAO;
	
	public TreeNode getUserTree(){
		List<BaseStaticTreeNode> nodeList = new ArrayList<BaseStaticTreeNode>();
		
		//设置根节点为用户树类型为base节点id为VirtualBaseTree
		nodeList.add(new BaseStaticTreeNode("VirtualBaseTree", "用户树", null, "base"));
		List<SysUser> userList = sysUserDAO.queryUser();
		
		if(userList != null && userList.size() !=0){
			for(SysUser userDTO: userList){
				//二级子节点为各用户 其父节点为根节点
				BaseStaticTreeNode treeNode = new BaseStaticTreeNode(String.valueOf(userDTO.getId()), userDTO.getName(), "VirtualBaseTree", "user");
				nodeList.add(treeNode);
			}
		}
		String id = null;
		StaticTree tree = new StaticTree(nodeList.toArray(new BaseStaticTreeNode[0]),id);
		tree.render();
		return tree.getRootNode();
	}
	
	
	public TreeNode getNodeTree(){
		List<BaseStaticTreeNode> nodeList = new ArrayList<BaseStaticTreeNode>();
		SysNode baseNode = sysNodeDAO.queryBaseNode();
		nodeList.add(new BaseStaticTreeNode(String.valueOf(baseNode.getId()), baseNode.getName(), null, "base"));
		List<SysNode> nodes = sysNodeDAO.queryNodeHasPid();
		if(nodes !=null && nodes.size() !=0){
			for(SysNode node:nodes){
				BaseStaticTreeNode treeNode = new BaseStaticTreeNode(String.valueOf(node.getId()), node.getName(), String.valueOf(node.getPId()), "nodes");
				nodeList.add(treeNode);
			}
		}
		String id = null;
		StaticTree tree = new StaticTree(nodeList.toArray(new BaseStaticTreeNode[0]),id);
		tree.render();
		return tree.getRootNode();
	}
	
}
